package com.zzh.fullscreen;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.LinkedList;
import java.util.List;

import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;

import mymusic.MyMusic;

public class MainDemoTest {
	
	public static void main(String[]args)
	{
			new MyWindow("My Window");
	}

}
class MyWindow extends JFrame
{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public MyWindow(String tile)
	{
		super(tile);
		DrawTest dt=new DrawTest();
		this.setBounds(100, 200, dt.getWidth()+12, dt.getHeight()+36);
		 this.setTitle("The Flyer Ballt"); 
		 this.setResizable(false);
		 this.setLayout(null);
		this.setDefaultCloseOperation(EXIT_ON_CLOSE);

//		di.setLayout(new BorderLayout());

		
//		this.addKeyListener(dt);
		dt.addKeyListener(new KeyAdapter()
				{
					public void keyPressed(KeyEvent ke) {
				// TODO Auto-generated method stub
				
//						if(dt.isOver==true&&ke.getKeyCode()==KeyEvent.VK_SPACE)
//						{
//							
//							dt.reset();;
						}
//						if(dt.isOver!=true&&ke.getKeyCode()==KeyEvent.VK_J)
//						{	
//							
////							new Thread(dt.music1).start();
//						}
						
//					}
				
				});
		this.add(dt);
		new Thread(dt).start();
		this.setVisible(true);
//		锟斤拷取锟斤拷锟斤拷 锟斤拷锟斤拷锟斤拷应锟斤拷锟斤拷锟铰硷拷
		dt.requestFocus();
	}
	
}

class DrawTest extends JPanel implements /*KeyListener,*/Runnable
{
	/**
	 * 
	 */
//	private int x=0;
//	private int y=0;
	private int hy1=0;
	private int hy2=0;
	private int width=0;
	private int height=0;
	private long fps=0;
	private BufferedImage back_img=null;
	
	boolean isOver=false;
	private static final long serialVersionUID = 1L;
	
	Player player;
	
	private  List<AirPlane> airPlanes=new LinkedList<AirPlane>();
//	
//	MyMusic music1=new MyMusic("C:\\Users\\47606\\Pictures\\music\\arrow_shot.wav",0);
	MyMusic bg_music=new MyMusic(DrawTest.class.getClassLoader().getResource("music/music.mid").getPath(),1);
	
	
	
	public DrawTest()
	{
		try
		{
			System.out.println("Loading->music.mid");
			InputStream fis=DrawTest.class.getClassLoader().getResourceAsStream("img/img_bg_level_1.jpg");
			System.out.println("Loading->background.jpg");
		
			back_img=ImageIO.read(fis);
			width=back_img.getWidth();
			height=back_img.getHeight();
			
			hy1=-height;
			
			this.setSize(width, height);
			player=new Player(200,300);
			this.addKeyListener(player);
			
		}
		catch(IOException ioe)
		{
			ioe.printStackTrace();
		}
		//new Thread(bg_music).start();
	}
	
	public void paintAirPlane(Graphics g){
		for(AirPlane a:airPlanes){
			a.step();
			a.paint(g);
		}
		for(int i=0;i<airPlanes.size();i++){
			AirPlane a=airPlanes.get(i);
			if(a.getY()>this.back_img.getHeight()){
				airPlanes.remove(a);
			}
		}
	}
	public void paintComponent(Graphics g)
	{
		super.paintComponent(g);
//		Graphics2D g2d=(Graphics2D)g;
		
//		g.setColor(Color.blue);
//		g.draw3DRect(10, 10, 100, 50, true);
//		g.setColor(Color.green);
//		g.drawOval(150, 50, 100, 100);
//		
//		g.setColor(Color.RED);

		if(hy1>=0)
			hy1=-height;
		if(hy1<=0&&hy2>=-height)
		g.drawImage(back_img, 0, hy1+=1, this);
		
		hy2=hy1+height;
		if(hy2>=0&&hy2<=height)
			g.drawImage(back_img, 0, hy2, this);
		g.setColor(Color.RED);
//		
		if(player.velx>0){			
			g.drawImage(player.image,player.x+10,player.y,player.width-20,player.height,this);
		}else if(player.velx<0){
			g.drawImage(player.image,player.x+10,player.y,player.width-20,player.height,this);
		}else if(player.velx==0)
				{
					g.drawImage(player.image,player.x,player.y,player.width,player.height,this);
					g.drawRect(player.x,player.y,player.width,player.height);
				}
		

		for(int i=0;i<airPlanes.size();i++)
		{	
			AirPlane ap=airPlanes.get(i);
//			b.updataXY();
			if(ap.y<=768)
			{
//		
				BufferedImage img=ImageOperate.rotateImage(AirPlane.airPlaneimage[0], 180);
				g.drawImage(img, ap.x , ap.y,ap.width,ap.height, this);
				g.drawRect(ap.x , ap.y,ap.width,ap.height);
			}

			else
			{
				airPlanes.remove(i);
			}
//		
			for(int x=0;x<ap.getBulletList().size();x++)
			{
				Bullet b=ap.getBulletList().get(x);
//			
				BufferedImage img=ImageOperate.rotateImage(Bullet.image[0], 180);
//				g.drawImage(img, b.x , b.y,b.width,b.height, this);
//				g.drawRect( b.x , b.y,b.width,b.height);
//				
				for(int y=0;y<player.getBulletList().size();y++)
				{	
					Bullet ab=player.getBulletList().get(y);
//					
					if(b.intersectTo(ab))
					{
						ap.getBulletList().remove(x);
						player.getBulletList().remove(y);
					}								
				}
//			
				if(b.intersectTo(player))
				{
					ap.getBulletList().remove(x);
					player.resetPosition();
					 
					 if(player.getLife()>=0)
						 player.setLife();		
				}
			}
			
			
			 if(ap.intersectTo(player))
			 {
				 airPlanes.remove(ap);
				 player.resetPosition();
				 
				 if(player.getLife()>=0)
					 player.setLife();				 
			 }
		}

		for(int i=0;i<player.getBulletList().size();i++)
		{	
			Bullet b=player.getBulletList().get(i);
//			b.updataXY();
			if(b.y+b.height[1]>=0&&b.y<=768)
				{
					g.drawImage(Bullet.image[1], b.x , b.y, this);
					g.drawRect( b.x , b.y,b.width[1],b.height[1]);
				}
			
			else
			{
				player.getBulletList().remove(b);
			}
			
			for(int p=0;p<airPlanes.size();p++)
			{	
				AirPlane a=airPlanes.get(p);
//				if(b.x<a.x+a.width&&b.y<a.y+a.height&&b.x+b.width>a.x&&b.y+b.height>a.y)
				if(b.intersectTo(a))
				{
					player.setScoer(a.getScore());
					player.getBulletList().remove(i);
					airPlanes.remove(p);
					
				}
			}
			
		}


		Font f=new Font(null,Font.ITALIC,16);
		g.setColor(Color.ORANGE);
		g.setFont(f);
		g.drawString("FPS :"+String.valueOf(fps),0,16);
		g.drawString("Score:"+String.valueOf(player.getScore()),0,32);
		g.drawString("PlayerLife:"+String.valueOf(player.getLife()),0,48);
		g.drawString("Bullets:"+String.valueOf(player.getBulletList().size()),0,64);
		g.drawString("AirPlanes:"+String.valueOf(airPlanes.size()),0,80);
		
		 if(this.isOver==true)
		 {
			 	f=new Font(null,Font.BOLD,26);
				g.setColor(Color.red);
				g.setFont(f);
				g.drawString("GAME OVER!",210,350);
		 }
//		g.drawRect(x,y,80,60);
//		g.setFont(Font.getFont(getName()));
//		String fo=Font.getFont(getName()).toString();
//		g.drawString(fo,150,20);
		
//		Font f=new Font("锟斤拷锟斤拷",Font.ITALIC,14);
//		g.setFont(f);
//		
//		GraphicsEnvironment env=
//				GraphicsEnvironment.getLocalGraphicsEnvironment();
//		String []fontNames=env.getAvailableFontFamilyNames();
//		int line=10;
//		for(String st:fontNames)
//		{
//			g.drawString(st,75,line+=20);
//		}
		
//		g.drawString("hello word!",75,100);
//		g.drawString(String.valueOf(getSize().height),0,40);
//		g.drawString(String.valueOf(getSize().width),0,60);
		
//		Polygon p=new Polygon();
//		
//		p.addPoint(10, 10);
//		p.addPoint(100, 30);
//		p.addPoint(50, 50);
//		p.addPoint(100, 70);
//		p.addPoint(30, 100);
//		g.setColor(Color.ORANGE);
//		g.drawPolygon(p);
//		p.translate(150, 0);
//		
//		g.fillPolygon(p);
	}
	public void reset()
	{
		this.airPlanes.clear();
		player.rest();
		isOver=false;
	}
	@Override
//	public void keyPressed(KeyEvent ke) {
//		// TODO Auto-generated method stub
//		
//		if(isOver==true&&ke.getKeyCode()==KeyEvent.VK_SPACE)
//		{
//			
//			this.airPlanes.clear();
//			player.rest();
//			isOver=false;
//		}
//	}
	
//	public void keyReleased(KeyEvent ke) {
		// TODO Auto-generated method stub
//		if(ke.getKeyCode()==KeyEvent.VK_A)
//		{
//			velx=0;
//		}
//		else if(ke.getKeyCode()==KeyEvent.VK_D)
//		{
//			velx=0;
//		}
//		if(ke.getKeyCode()==KeyEvent.VK_W)
//		{
//			vely=0;
//		}
//		else if(ke.getKeyCode()==KeyEvent.VK_S)
//		{
//			vely=0;
//		}
//		
//	}
//	
//	public void keyTyped(KeyEvent arg0) {
//		// TODO Auto-generated method stub
//		
//	}
	
	public void run()
	{
		long start=0;
		long end=0;
		int tfps=0;
		start=System.currentTimeMillis();
		while(true)
		{
			try
			{
				Thread.sleep(20);
				if(player.getLife()>=0)
				{
					
					player.step();
//				
				}
				else{
					isOver=true;
				}

				
//			
				this.repaint();
				end=System.currentTimeMillis();
				tfps++;
				if(end-start>=1000)
				{
					if(isOver!=true)
					{
						AirPlane ap=new AirPlane( 40*(int)( Math.random()*12),-128,1);
						airPlanes.add(ap);
					}
					fps=tfps;
					tfps=0;
					start=System.currentTimeMillis();
				}
				
			}
			catch(InterruptedException ie)
			{
				ie.printStackTrace();
			}
		}
	}
}
